<template>
	<view :style="{height:PhoneInfo.windowHeight + 'px'}">
		<mvBar :mysNavConfig="mysNavConfig" @onLeftClick="onLeftClick"></mvBar>
		<!-- 消息提示 -->
		<u-toast ref="uToast"></u-toast>
		<view class="u-search-box  cssBox">
			<view class="u-search-inner">
				<u-search v-model="searchKeyword" @search="this.getequipmententrust()" shape="round" :clearabled="true"
					:show-action="true" actionText="新增" @custom="gotoAdd" input-align="left">
				</u-search>
			</view>
		</view>
		<view style="height: 82.5%; overflow: hidden;">
			<scroll-view scroll-y="true" style="height: 100%;" @scrolltolower="scrolltolower">
				<uni-list v-for="(item,index) of userlist" :key="index">
					<uni-list-item @click="gotomanage(item)" :clickable='true'>
						<template v-slot:header>
							<view class="slot-box">
								<image class="slot-image" src="/static/img//userCenter/entrust.png" mode="widthFix">
								</image>
							</view>
						</template>
						<template v-slot:body>
							<text class="slot-box slot-text">{{item.userName}}</text>
						</template>
						<template v-slot:footer>
							<u-tag text="设备管理" plain plainFill @click="gotomanage(item)"></u-tag>
						</template>
					</uni-list-item>
				</uni-list>
				<u-loadmore :status="loadStatus" :loading-text="loadingText" :loadmore-text="loadmoreText"
					nomore-text=' ' bgColor="#efeff4"></u-loadmore>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				PhoneInfo: '',
				mysNavConfig: {
					/* 占位开启  */
					navPadding: true,
					/* 背景 */
					bgColor: "#2979FF",
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "设备授权",
						color: "#ffffff",
						fontSize: "18px", // px upx rpx
						fontWeight: "500", // 100 - 700
					},
					isCustomFn: true,
					btnType: "type2",
					type2Config: {
						// 左图标
						leftPath: "/static/img/back.png",
						// 右图标
						rightPath: "/static/img/home.png",
						// 圆角
						radius: "40rpx",
					}
				},
				userinfo: '',
				userlist: [],
				searchKeyword: '',
				loadStatus: 'loadmore',
				loadingText: '加载中...',
				loadmoreText: '上拉加载更多',
				pageIndex: '1',
				pageSize: '20',
				count: '',
			}
		},
		onLoad() {
			this.PhoneInfo = this.$utils.GetPhoneInfo();
			this.userinfo = uni.getStorageSync('userinfo');
			this.getequipmententrust()
		},
		methods: {
			getequipmententrust() {
				uni.showLoading({
					title: "加载中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Uri_lookentrustuser.replace(
						"{userRealId}",
						this.userinfo.userRealId),
					data: {
						page: this.pageIndex,
						limit: this.pageSize,
						UserName: this.searchKeyword,
					},
					dataType: "json",
					method: "GET",
					success: (res) => {
						if (res.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (res.data.code == 0) {
							this.count = res.data.count
							this.userlist = res.data.data
							if (this.count < this.pageSize) {
								this.loadStatus = "nomore";
							} else { //否则就是还有数据
								this.loadStatus = "loadmore";
							}
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail() {
						this.$utils.showToast(this, "加载失败！", 'error');
					},
					complete() {
						uni.hideLoading();
					}
				})
			},
			//返回
			onLeftClick() {
				uni.switchTab({
					url: '/pages/tabbar/userCenter'
				});
			},
			//新增用户授权
			gotoAdd() {
				uni.redirectTo({
					url: '/pages/entrust/addEntrustuser'
				})
			},
			//授权管理
			gotomanage(value) {
				uni.redirectTo({
					url: '/pages/entrust/entrustManage?userid=' + value.userRealId + '&name=' + value.userName
				})
			},
			//加载
			scrolltolower() {
				if (this.count < this.pageSize) {
					this.loadStatus = "nomore";
				} else {
					setTimeout(() => {
						this.loadStatus = "loading";
						this.pageSize += 20;
						this.getequipmententrust();
					}, 500);
				}
			},
		},
		//下拉刷新
		onPullDownRefresh() {
			this.pageSize = 20;
			this.getequipmententrust();
			uni.stopPullDownRefresh();
		}
	};
</script>

<style lang="scss">
	.cssBox {
		padding: 20rpx 30rpx;
		background-color: #FFFFFF;
		margin-bottom: 15rpx;
	}

	.slot-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}

	.slot-image {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}

	.slot-text {
		flex: 1;
		font-size: 14px;
		margin-right: 10px;
	}
</style>